package com.jiepos.mpos.biz.analysis.service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.LogTransJYLSDao;
import com.jiepos.mpos.common.entity.ChartEntity;
import com.jiepos.mpos.core.util.CollectionUtil;
import com.jiepos.mpos.core.util.DateUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 *
 *
 * <p>标题: —— 统计交易流水类</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos]
 * @Package com.jiepos.mpos.biz.analysis.service
 * @author 余翼膑
 * @version 1.0
 * @since 2017年10月8号
 * @history（历次修订内容、修订人、修订时间等）
 */
@Service
public class TranasctionLogService {

    @Autowired
    private LogTransJYLSDao logTransJYLSDao;

    /**
     * 查询交易流水
     * 查询时间默认当日以内的前一个月
     * @return
     */
    public List getCustomList(HttpServletRequest request,String loc_dt_start, String loc_dt_end, String ins_cd, String ins_cn_nm,String service_param_uuid,String service_uuid ) {
        String ins_phone = String.valueOf(request.getSession().getAttribute(SystemConstant.SESSION_LOGIN_INS_PHONE));
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append(" SELECT to_char(sum(t1.trans_at),'fm9999999990.00')  as sumTranAt, TO_CHAR(TO_DATE(t1.loc_dt, 'YYYY-MM-DD'), 'yyyy-mm-dd') as dt ")
                .append("   FROM "+ DBConstant.Table.T_APP_LOG_TRANS_JNLS+" t1 ")
                .append("   left join "+ DBConstant.Table.T_APP_INS_INF+" t2 ")
                .append("     on t1.ins_uuid = t2.ins_uuid ")
                .append(" left join "  + DBConstant.Table.T_APP_SERVICE_PARAM + " t3 ")
                .append(" on t1.service_param_uuid = t3.service_param_uuid ")
                .append("  where t1.rspcode ='00'" );

        List param = new ArrayList();
        //机构号
        if (!StringUtils.isBlank(ins_cd)) {
            sqlBuf.append( " and t2.ins_cd like ? " );
            param.add("%" + ins_cd + "%");
        }
        //机构名称
        if (!StringUtils.isBlank(ins_cn_nm)) {
            sqlBuf.append( " and t2.ins_cn_nm like ? " );
            param.add("%" + ins_cn_nm + "%");
        }
        //起止日期
        if (!StringUtils.isBlank(loc_dt_start)) {
            Date dateParam = DateUtil.strToDate(loc_dt_start, DateUtil.FORMAT_DATE_1);
            sqlBuf.append("and t1.loc_dt >= ? ");
            param.add(DateUtil.dateToStr(dateParam, "yyyyMMdd"));
        }/* else {
            sqlBuf.append(" and t1.loc_dt >= ? ");
            String strDate = DateUtil.getAddDaysDate(new Date(), -31);
            Date date =  DateUtil.strToDate(strDate);
            param.add(DateUtil.dateToStr(date, "yyyyMMdd"));
        }*/
        if (!StringUtils.isBlank(loc_dt_end)) {
            Date dateParam = DateUtil.strToDate(loc_dt_end, DateUtil.FORMAT_DATE_1);
            sqlBuf.append("and t1.loc_dt <= ? ");
            param.add(DateUtil.dateToStr(dateParam, "yyyyMMdd"));
        } /*else {
            sqlBuf.append(" and t1.loc_dt <= ? ");
            param.add(DateUtil.dateToStr(new Date(), "yyyyMMdd"));
        }*/
        //机构名称
        if (!StringUtils.isBlank(service_uuid)) {
            sqlBuf.append("and t1.service_uuid = ? ");
            param.add(service_uuid);
        }
        //业务参数名称
        if (!StringUtils.isBlank(service_param_uuid)) {
            sqlBuf.append("and t1.service_param_uuid = ? ");
            param.add(service_param_uuid);
        }

        sqlBuf.append(" and  t1.custom_uuid in ");
        sqlBuf.append(" (select tt.custom_uuid from "+ DBConstant.Table.T_APP_CUSTOM_INF+" tt ");
        sqlBuf.append(" start with tt.login_id='" + ins_phone + "'");
        sqlBuf.append(" connect by prior tt.custom_uuid = tt.sup_custom_uuid)");
        sqlBuf.append("  group by TO_CHAR(TO_DATE(t1.loc_dt, 'YYYY-MM-DD'), 'yyyy-mm-dd') ")
                .append("  ORDER BY TO_CHAR(TO_DATE(t1.loc_dt, 'YYYY-MM-DD'), 'yyyy-mm-dd') ASC ");
        List<Map<String, Object>> list = logTransJYLSDao.queryForList(sqlBuf.toString(), param);
        if(CollectionUtil.isEmpty(list)){
            return list;
        }
        return list;
    }

    /**
     * 单个图标系列
     */
    public String getTransactionLogList(HttpServletRequest request, String loc_dt_start, String loc_dt_end, String ins_cd, String ins_cn_nm, String service_param_uuid, String service_uuid) throws Exception {
        ChartEntity chartEntity = new ChartEntity();
        chartEntity.setCaption("会员日交易汇总统计");
        chartEntity.setxAxisName("日期("+loc_dt_start+"~"+loc_dt_end+")");
        /*chartEntity.setxAxisName("日期("+DateUtil.getAddDaysDate(new Date(), -31)+"~"+DateUtil.dateToStr(new Date(), "yyyy-MM-dd")+")");*/
        chartEntity.setyAxisName("金额(元)");
        chartEntity.setShowValues("1");
        chartEntity.setDecimalPrecision("1");
        chartEntity.setFormatNumberScale("0");
    //    chartEntity.setFormatNumberScale("1");
        StringBuffer strXML = new StringBuffer();
        strXML.append("<graph  useRoundEdges='1'  caption='" + chartEntity.getCaption() + "' forceDecimals='1' xAxisName='"
                + chartEntity.getxAxisName() + "' yAxisName='" + chartEntity.getyAxisName() + "' FormatNumberScale='"+chartEntity.getFormatNumberScale() + "' DecimalPrecision= '"
                + chartEntity.getDecimalPrecision() +"'  "
                + " yAxisMinValue = '" + "" + "' yAxisMaxValue = '"+ "" + "'>");
        List<Map<String, Object>> dataList  = this.getCustomList(request,loc_dt_start,loc_dt_end,ins_cd,ins_cn_nm,service_param_uuid,service_uuid);
        DecimalFormat decimalFormat = new DecimalFormat("########.00");  
        for (int i = 0; i < dataList.size(); i++) {
            Map row = dataList.get(i);
            String value  = decimalFormat.format(Double.parseDouble(String.valueOf(row.get("sumTranAt"))));
            String name  = String.valueOf(row.get("dt"));
            
           strXML.append("<set label='" + name +"' " + "value='" + value +"'>");
            //strXML.append("<set label='" + name +"' " + "value='12323232323.23'>");
            strXML.append("</set>");
        }
        strXML.append("</graph>");
        String str =  strXML.toString();
        return str;
    }
}
